﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// BaseClass 的摘要说明
/// </summary>
public class BaseClass
{
    protected BaseClass()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary> 
    /// 更新数据
    /// </summary> 
    /// <param name="n">Hashtable</param>
    /// <param name="TableName">表名</param>
    /// <returns>返回受影响行数</returns>
    protected int BaseUpdate(Hashtable n, string TableName)
    {
        string updateColumns = string.Empty;
        foreach (string key in n.Keys)
        {
            if (key == "id")
                continue;
            if (updateColumns != string.Empty)
                updateColumns += ", ";
            updateColumns += string.Format("{0}=@{0}", key, key);
        }
        string sql = string.Format("update " + TableName + " set {0} where id=@id", updateColumns);
        return new SQLHelper("getnum").Execute(sql, n);
    }
    /// <summary> 
    /// 更新数据
    /// </summary> 
    /// <param name="n">Hashtable</param>
    /// <param name="TableName">表名</param>
    /// <param name="UserNum">用户账号</param>
    /// <returns>返回受影响行数</returns>
    protected int BaseUpdate_UserNum(Hashtable n, string TableName, string UserNum)
    {
        string updateColumns = string.Empty;
        foreach (string key in n.Keys)
        {
            if (key == UserNum)
                continue;
            if (updateColumns != string.Empty)
                updateColumns += ", ";
            updateColumns += string.Format("{0}=@{0}", key, key);
        }
        string sql = string.Format("update " + TableName + " set {0} where "+ UserNum + "=@"+ UserNum + "", updateColumns);
        return new SQLHelper("getnum").Execute(sql, n);
    }

    /// <summary>
    /// 新增数据
    /// </summary>
    /// <param name="n"></param>
    /// <param name="TableName">表名</param>
    /// <returns></returns>
    protected int BaseInsert(Hashtable n, string TableName)
    {
        string insertColumns = string.Empty;
        string insertParams = string.Empty;
        foreach (string key in n.Keys)
        {
            if (insertColumns != string.Empty)
            {
                insertColumns += ", ";
                insertParams += ", ";
            }
            insertColumns += string.Format("{0}", key);
            insertParams += string.Format("@{0}", key);
        }
        string sql = string.Format("insert into " + TableName + " ({0}) values ({1}) ;", insertColumns, insertParams);
        return new SQLHelper("getnum").Execute(sql, n);
    }

    /// <summary>
    /// 删除一条
    /// </summary>
    /// <param name="id"></param>
    /// <param name="TableName">表名</param>
    /// <returns></returns>
    protected int BaseDelete(string id, string TableName)
    {
        string sql = "delete from " + TableName + " where 1 = 1 and id = " + id;
        return new SQLHelper("getnum").Execute(sql);
    }
    /// <summary>
    /// 删除-批量
    /// </summary>
    /// <param name="ids"></param>
    /// <param name="TableName"></param>
    /// <returns></returns>
    protected int BaseDeleteBatch(string ids, string TableName)
    {
        string sql = "delete from " + TableName + " where 1 = 1 and id in (" + ids + ")";
        return new SQLHelper("getnum").Execute(sql);
    }
    /// <summary>
    /// 传入查询参数实体条件 获取model
    /// </summary>
    /// <param name="n"></param>
    /// <param name="TableName">表名</param>
    /// <returns></returns>
    protected Hashtable BaseGetModel(Hashtable n, string TableName)
    {
        string sql = "select * from " + TableName + " where 1=1 ";
        foreach (string key in n.Keys)
        {
            sql += string.Format(" and {0}=@{0} ", key);
        }
        ArrayList data = new SQLHelper("getnum").Select(sql, n);
        return data.Count > 0 ? (Hashtable)data[0] : new Hashtable();
    }
}